﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace HungTrung
{
    public partial class Form1 : Form
    {
       private Timer timeplay ;
        private Ro _ro;
        public Form1()
        {
            this.KeyPreview = true;
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //tao 1 doi tuong Ro ()
            _ro = new Ro();
            _ro.AddtoForm(this);
            _ro.Left = this.Width / 2;
            _ro.Top =this.Height - 110;

            //tao truoc 1 con ga
            GiaCam giacam1;
            giacam1 = new GiaCam('g');

            giacam1.Top  = 50;
            giacam1.Left = 100;
            
            giacam1.AddtoForm(this);
            //tao 1 doi tuong timer
            timeplay = new Timer();
            timeplay.Interval = 500;
            timeplay.Tick+= new EventHandler(ThaTrung);

        }

        private void ThaTrung(object sender, EventArgs e)
        {
            for (int i = 0; i < GiaCam.ListGiaCam.Count(); i++)
            {
                System.TimeSpan timediff = System.DateTime.Now.Subtract(GiaCam.ListGiaCam[i].timebatdau);
                GiaCam.ListGiaCam[i].updatvitri();
                if (timediff.TotalMilliseconds>=5000)
                {
               GiaCam.ListGiaCam[i].thatrung(this);
               GiaCam.ListGiaCam[i].Solanroi += 1;
               GiaCam.ListGiaCam[i].timebatdau = System.DateTime.Now;
                }
            }
            
        }
        

        private void buttonBatDau_Click(object sender, EventArgs e)
        {
            timeplay.Start();
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            timeplay.Stop();
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            _ro.Left = e.X;
            if (_ro.Left > 600)
                _ro.Left = 600;
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
      
        }
      

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
             int index= GiaCam.ListGiaCam.Count();
            GiaCam giacam;
            if (e.KeyCode == Keys.G)
            {
                 giacam = new GiaCam('g');
                giacam.Top = 50;
                giacam.Left= GiaCam.ListGiaCam[index-1].Left+97;
                giacam.AddtoForm(this);

            }
            

            if (e.KeyCode == Keys.V)
            {
                 giacam = new GiaCam('v');
                giacam.Top = 50;
                giacam.Left = GiaCam.ListGiaCam[index - 1].Left+97;
                giacam.AddtoForm(this);
            }


            if (e.KeyCode == Keys.N)
            {
                 giacam = new GiaCam('n');
                giacam.Top = 50;
                giacam.Left = GiaCam.ListGiaCam[index - 1].Left+97;
                giacam.AddtoForm(this);
            }


            if (e.KeyCode == Keys.Left)
            {
                if (_ro.Left - 10 > 0)
                    _ro.Left -= 10;

            }
                
 
        }

       
    }
}
